package com.workManager.controller;

import com.github.pagehelper.PageInfo;
import com.wordnik.swagger.annotations.ApiOperation;
import com.workManager.base.BaseController;
import com.workManager.config.RespCode;
import com.workManager.pojo.TdFunction;
import com.workManager.pojo.TdMenu;
import com.workManager.service.MenuService;
import com.workManager.service.MenuServiceImpl;
import com.workManager.vo.MenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by Thinkpad on 2017/2/16 0016.
 */
@Controller
@RequestMapping("/menu")
public class MenuController extends BaseController{

    @Autowired
    private MenuService menuService;

    @ApiOperation(value="菜单添加",notes = "菜单添加")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public  Object add(HttpServletRequest request, @RequestBody TdMenu menu){
        ModelMap modelMap = new ModelMap();
        menu.setCreateDate(new Date());
        menu.setIsValid(true);


        Map<String,Object> map = new HashMap<String, Object>();
        map.put("menuName",menu.getMenuName());
        PageInfo<MenuVo> menuPageInfo = menuService.query(map);

        Map<String,Object> map1 = new HashMap<String, Object>();
        map1.put("menuUrl",menu.getMenuUrl());
        PageInfo<MenuVo> menuPageInfo1 = menuService.query(map1);

        Map<String,Object> map2 = new HashMap<String, Object>();
        map2.put("viewPermission",menu.getViewPermission());
        PageInfo<MenuVo> menuPageInfo2 = menuService.query(map2);

        if(menuPageInfo.getTotal()>0){
            return setModelMap(modelMap, RespCode.INTERNAL_SERVER_ERROR,"菜单名："+menu.getMenuName()+"已存在！");
        }
        if(menuPageInfo1.getTotal()>0){
            return setModelMap(modelMap,RespCode.INTERNAL_SERVER_ERROR,"菜单url:"+menu.getMenuUrl()+"已存在！");
        }
        if(menuPageInfo2.getTotal()>0){
            return setModelMap(modelMap,RespCode.INTERNAL_SERVER_ERROR,"浏览权限："+menu.getViewPermission()+"已存在！");
        }

        menuService.add(menu);
        return  setSuccessModelMap(modelMap,"新增成功");
    }

    @ApiOperation(value="删除菜单",notes = "删除菜单")
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public Object delete(HttpServletRequest request, @RequestParam(value = "menuId",required = false) String menuId){
        ModelMap modelMap = new ModelMap();
        menuService.delete(menuId);
        return  setSuccessModelMap(modelMap);
    }

    @ApiOperation(value="批量删除菜单",notes = "批量删除菜单")
    @RequestMapping(value = "/deleteByList",method = RequestMethod.POST)
    public Object deleteByList(HttpServletRequest request, @RequestParam(value = "menuIds",required = false) List<String> menuIds){
        ModelMap modelMap = new ModelMap();
        menuService.deleteByList(menuIds);
        return  setSuccessModelMap(modelMap);
    }

    @ApiOperation(value="菜单更新",notes = "菜单更新")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Object update(HttpServletRequest request, @RequestBody TdMenu menu){
        ModelMap modelMap = new ModelMap();
        menu.setIsValid(true);

        Map<String,Object> map = new HashMap<String, Object>();
        map.put("menuId",menu.getMenuId());

        PageInfo<MenuVo> menuPageInfo = menuService.query(map);

        MenuVo menu1 = menuPageInfo.getList().get(0);
        if(menu1.getMenuName()!=null&&!menu1.getMenuName().equals(menu.getMenuName())){

            Map<String,Object> map1 = new HashMap<String, Object>();
            map1.put("menuName",menu.getMenuName());
            PageInfo<MenuVo> menuPageInfo1 = menuService.query(map1);
            if(menuPageInfo1.getTotal()>1){
                return  setModelMap(modelMap,RespCode.INTERNAL_SERVER_ERROR,"菜单名："+menu.getMenuName()+" 已经存在！");
            }
        }
        if(menu1.getMenuUrl()!=null && !menu1.getMenuUrl().equals(menu.getMenuUrl())){
            Map<String,Object> map1 = new HashMap<String, Object>();
            map1.put("menuUrl",menu.getMenuUrl());
            PageInfo<MenuVo> menuPageInfo1 = menuService.query(map1);
            if(menuPageInfo1.getTotal()>1){
                return  setModelMap(modelMap,RespCode.INTERNAL_SERVER_ERROR,"菜单URL："+menu.getMenuUrl()+" 已经存在！");
            }
        }
        if(menu1.getViewPermission() !=null && !menu1.getViewPermission().equals(menu.getViewPermission())){
            Map<String,Object> map1 = new HashMap<String, Object>();
            map1.put("viewPermission",menu.getViewPermission());
            PageInfo<MenuVo> menuPageInfo1 = menuService.query(map1);
            if(menuPageInfo1.getTotal()>1){
                return  setModelMap(modelMap,RespCode.INTERNAL_SERVER_ERROR,"菜单浏览权限："+menu.getViewPermission()+" 已经存在！");
            }
        }

        menuService.update(menu);
        return  setSuccessModelMap(modelMap,"修改成功");
    }

    @ApiOperation(value="菜单查询",notes = "菜单查询")
    @RequestMapping(value = "/query",method = RequestMethod.POST)
    public Object query(HttpServletRequest request,
                        @RequestParam(value = "pageNumber",required = false) String pageNumber,
                        @RequestParam(value = "pageSize",required = false) String pageSize,
                        @RequestParam(value="menuName", required = false) String menuName){
        ModelMap modelMap = new ModelMap();

        Integer pagenum = pageNumber==null?1:Integer.parseInt(pageNumber);
        Integer pagesize = pageSize==null?10:Integer.parseInt(pageSize);


        Map<String,Object> queryParam = new HashMap<String, Object>();
        queryParam.put("pagenum",pagenum);
        queryParam.put("pagesize",pagesize);
        queryParam.put("menuName",menuName);

        PageInfo<MenuVo> tdMenuPageInfo = menuService.queryLikeMeunName(queryParam);

        Map<String,Object> dataMap = new HashMap<String, Object>();
        dataMap.put("total",tdMenuPageInfo.getTotal());
        dataMap.put("rows",tdMenuPageInfo.getList());
        return  setSuccessModelMap(modelMap,dataMap);
    }

    @ApiOperation(value = "查询父级菜单", notes = "查询父级菜单")
    @RequestMapping(value = "/queryParentMenu",method = RequestMethod.POST)
    public Object queryParentMenu(HttpServletRequest request){
        ModelMap modelMap = new ModelMap();
        List<MenuVo> meList = menuService.queryParentMenu();
       return setSuccessModelMap(modelMap,meList);


    }

    @ApiOperation(value="菜单查询",notes = "按角色权限返回")
    @RequestMapping(value = "/queryFunctionMenu",method = RequestMethod.POST)
    public Object queryFunctionMenu(@RequestParam(value="loginCode") String loginCode){
        ModelMap modelMap = new ModelMap();
        List<Map<String,Object>> dataMap = new ArrayList<Map<String, Object>>();

        List<MenuVo> menuList =   menuService.queryFunctionMenu(loginCode);

        //获取一级菜单
        Set<String> set = new LinkedHashSet<String>();
        for (MenuVo menuVo:menuList){
            set.add(menuVo.getParentMenuName());
        }
        Iterator s = set.iterator();
        while (s.hasNext()){
            Map map = new HashMap();
            //保存子菜单
            List<MenuVo> list = new ArrayList<MenuVo>();
            Map menuMap = new HashMap();
            String parentMenuName = s.next().toString();
            for(MenuVo menuVo:menuList){
                if(parentMenuName.equals(menuVo.getParentMenuName())){
                    list.add(menuVo);
                }
            }

            menuMap.put("text",parentMenuName);
            menuMap.put("items",list);

            List<Map<String,Object>> mapList = new ArrayList<Map<String, Object>>();
            mapList.add(menuMap);

            map.put("id",list.get(0).getParentMenuUrl());
            map.put("homePage",list.get(0).getMenuUrl());
            map.put("menu",mapList);
            dataMap.add(map);
        }

        return  setSuccessModelMap(modelMap,dataMap);
    }



}
